package com.example.demo.mapper;

import com.example.demo.dto.UploadDownloadHistoryDTO;
import com.example.demo.model.UploadDownloadHistory;
import com.example.demo.utils.Page;
import org.apache.ibatis.annotations.*;
import org.springframework.data.domain.Limit;
import org.springframework.data.domain.Pageable;

import java.util.List;

import java.util.List;

@Mapper
public interface UploadDownloadHistoryMapper {
    @Insert("INSERT INTO upload_download_history (user_id, torrent_id, action_type, created_at) " +
            "VALUES (#{userId}, #{torrentId}, #{actionType}, #{createdAt})")
    void insertHistory(UploadDownloadHistory history);

    @Select("SELECT udh.id, udh.user_id, udh.torrent_id, t.file_name, udh.action_type, udh.created_at " +
            "FROM upload_download_history udh " +
            "JOIN torrent t ON udh.torrent_id = t.id " +
            "WHERE udh.user_id = #{userId} AND udh.action_type = #{actionType}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "torrentId", column = "torrent_id"),
            @Result(property = "filename", column = "file_name"),
            @Result(property = "actionType", column = "action_type"),
            @Result(property = "createdAt", column = "created_at")
    })
    List<UploadDownloadHistoryDTO> findByUserIdAndActionType(Long userId, String actionType);
    @Select("SELECT udh.*, u.username " +
            "FROM upload_download_history udh " +
            "LEFT JOIN user u ON udh.user_id = u.id " +
            "WHERE udh.torrent_id = #{torrentId} AND udh.action_type = 'DOWNLOAD'")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "torrentId", column = "torrent_id"),
            @Result(property = "filename", column = "file_name"),
            @Result(property = "actionType", column = "action_type"),
            @Result(property = "createdAt", column = "created_at"),
            @Result(property = "username", column = "username")
    })
    List<UploadDownloadHistory> findDownloadsByTorrentIdWithUsername(@Param("torrentId") Long torrentId);

}